﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Text;

namespace AcServerWrapper
{
    class FilenameHelper
    {
        /** 
         * Location of the AcServerWrapper directory
         */
        public static string WrapperDirectory
        {
            get
            {
                return Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
            }
        }

        /** 
         * Location of the AcServer directory
         */
        public static string ServerDirectory
        {
            get
            {
                string acServerLocation = ConfigurationManager.AppSettings["acServerLocation"];
                if (string.IsNullOrEmpty(acServerLocation))
                    throw new Exception("No appsetting 'acServerLocation' defined in 'AcServerWrapper.exe.config'. Please use a relative or absolute path, at least \".\"");

                string serverDirectory;

                if (Path.IsPathRooted(acServerLocation))
                    serverDirectory = acServerLocation;
                else
                {
                    serverDirectory = Path.Combine(WrapperDirectory, acServerLocation);
                }

                // That should be the location of the acserver. 
                if (!Directory.Exists(serverDirectory))
                    throw new DirectoryNotFoundException("Directory \"" + serverDirectory + "\" could not be found. Please check your 'acServerLocation' defined in 'AcServerWrapper.exe.config'");

                return serverDirectory;
            }
        }

        /**
         * Returns the executable filename of the acserver, check if existent included
         */
        public static string ServerExecutable
        {
            get
            {
                return GetServerPath(ConfigurationManager.AppSettings["executable"]);
            }
        }

        /** 
         * This returns a relative path to the wrapper binary. Use this for configs etc.
         */
        internal static string GetWrapperPath(string relativeOrAbolutePath)
        {
            return GetPathAndCheckHelper(WrapperDirectory, relativeOrAbolutePath);
        }

        /** 
         * This returns a relative path to the server binary
         */
        internal static string GetServerPath(string relativeOrAbolutePath)
        {
            return GetPathAndCheckHelper(ServerDirectory, relativeOrAbolutePath);
        }

        private static string GetPathAndCheckHelper(string root, string relativeOrAbolutePath)
        {
            if (!Directory.Exists(root))
                throw new DirectoryNotFoundException("Directory \"" + root + "\" doesn't exist");

            // If it's an absolute path, we are fine
            if (Path.IsPathRooted(relativeOrAbolutePath))
                return relativeOrAbolutePath;

            // Should be a relative path, so we combine this to the root parameter
            string relativePath = Path.Combine(root, relativeOrAbolutePath);
            if (!Directory.Exists(relativePath) && !File.Exists(relativePath))
                throw new FileNotFoundException("File or directory \"" + relativePath + "\" could not be found.");
            return relativePath;
        }
    }
}
